package com.xdtech.forum.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xdtech.forum.service.LinkTagService;
import com.xdtech.forum.vo.LinkTagItem;
import com.xdtech.web.model.Pagination;
import com.xdtech.web.model.ResultMessage;

/**
 * 
 * @author max.zheng
 * @create 2015-03-06 11:06:23
 * @since 1.0
 * @see
 */
@Controller
@Scope("prototype")
@RequestMapping("/linkTag.do")
public class LinkTagController {
	@Autowired
	private LinkTagService linkTagService;
	@RequestMapping(params = "linkTag")
	public ModelAndView skipLinkTag() {
		return new ModelAndView("forum/linkTag/linkTag_ftl");
	}
	
	@RequestMapping(params="loadList")
	@ResponseBody
	public Map<String, Object> loadList(HttpServletRequest request,Pagination pg) {
		Map<String, Object> results = null;
		if (StringUtils.isEmpty(request.getParameter("page"))) {
			//不分页处理
			results = linkTagService.loadPageAndCondition(null, null);
		}else {
			results = linkTagService.loadPageAndCondition(pg, null);
		}
		
		return results;
	}
	
	@RequestMapping(params = "editLinkTag")
	public ModelAndView editLinkTag(HttpServletRequest request,Long linkTagId) {
		if (linkTagId!=null) {  
			request.setAttribute("linkTagItem", linkTagService.loadLinkTagItem(linkTagId));
		}
		return new ModelAndView("forum/linkTag/editLinkTag_ftl");
	}
	
	@RequestMapping(params = "saveLinkTag")
	@ResponseBody
	public ResultMessage saveLinkTag(LinkTagItem item) {
		ResultMessage r = new ResultMessage();
		if (linkTagService.saveOrUpdateLinkTag(item)) {
			r.setSuccess(true);
		}else {
			r.setSuccess(false);
		}
		return r;
	}
	
	@RequestMapping(params = "deleteLinkTagItems")
	@ResponseBody
	public ResultMessage deleteLinkTagItems(String ids) {
		ResultMessage r = new ResultMessage();
		if (StringUtils.isNotEmpty(ids)) {
			String[] tempIds = ids.split(",");
			List<Long> linkTagIds = new ArrayList<Long>();
			for (String id : tempIds) {
				linkTagIds.add(Long.valueOf(id));
			}
			linkTagService.deleteLinkTagInfo(linkTagIds);
			r.setSuccess(true);
		}else {
			r.setSuccess(false);
		}
		return r;
	}

}
